package com.sansan.qiangji.realize.warehouse;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.sansan.qiangji.dto.PullOrderExcelDto;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class PullOrderExcelListener extends AnalysisEventListener<PullOrderExcelDto> {
    private PullOrderReal pullOrderReal;

    private List<PullOrderExcelDto> pullOrderExcelDtos = new ArrayList<>();

    private String batchNumber = "";

    public PullOrderExcelListener(PullOrderReal pullOrderReal){
        this.pullOrderReal = pullOrderReal;
    }

    @Override
    public void invoke(PullOrderExcelDto pullOrderExcelDto, AnalysisContext analysisContext) {
        ReadRowHolder readRowHolder = analysisContext.readRowHolder();
        int rowIndex = readRowHolder.getRowIndex()+1;
        if (pullOrderExcelDto.getBatchNumber() == null || pullOrderExcelDto.getBatchNumber().equals("")){
            log.error("【上传入库单】批次号不能为空");
            throw new ServiceException(ResultEnum.BATCH_NUMBER_NOT_EMPTY.getCode(), "第 "+ rowIndex +" 行批次号不能为空！");
        }
        if (pullOrderExcelDto.getProductCode() == null || pullOrderExcelDto.getProductCode().equals("")){
            log.error("【上传入库单】物料编号不能为空");
            throw new ServiceException(ResultEnum.PRODUCT_NAME_NOT_EMPTY.getCode(), "第 "+ rowIndex +" 行物料编号不能为空！");
        }
        if (pullOrderExcelDto.getProductName() == null || pullOrderExcelDto.getProductName().equals("")){
            log.error("【上传入库单】物料名称不能为空");
            throw new ServiceException(ResultEnum.PRODUCT_NAME_NOT_EMPTY.getCode(), "第 "+ rowIndex +" 行物料名称不能为空！");
        }
        if (pullOrderExcelDto.getUnintCount() == null || pullOrderExcelDto.getUnintCount().equals("")){
            log.error("【上传入库单】入库数量不能为空");
            throw new ServiceException(ResultEnum.UNINT_COUNT_NOT_EMPTY.getCode(), "第 "+ rowIndex +" 行入库数量不能为空！");
        }
        try {
            Integer.parseInt(pullOrderExcelDto.getUnintCount());
        } catch (NumberFormatException e) {
            log.error("【上传入库单】入库数量格式异常");
            throw new ServiceException(ResultEnum.UNINT_COUNT_FORMAT_ERROR.getCode(), "第 "+ rowIndex +" 行入库数量格式异常！");
        }
        if (batchNumber.equals("")){
            batchNumber = pullOrderExcelDto.getBatchNumber();
        }
        if (!pullOrderExcelDto.getBatchNumber().equals(batchNumber)){
            log.error("【上传入库单】存在多批次物料");
            throw new ServiceException(ResultEnum.OTHER_BATCH_NUMBER_EXIST.getCode(), "第 "+ rowIndex +" 行存在多批次物料！");
        }
        if (pullOrderExcelDtos.stream().anyMatch(e -> e.getProductCode().equals(pullOrderExcelDto.getProductCode()) || e.getProductName().equals(pullOrderExcelDto.getProductName()))){
            log.error("【上传入库单】存在同一物料");
            throw new ServiceException(ResultEnum.HAVE_SAME_PRODUCT.getCode(), "第 "+ rowIndex +" 行存在同一物料！");
        }
        pullOrderExcelDto.setPullOrderIndex(rowIndex);
        pullOrderExcelDtos.add(pullOrderExcelDto);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (pullOrderExcelDtos.size()!=0){
            pullOrderReal.uploadPullOrderExcel(pullOrderExcelDtos);
        }
    }
}
